<?php
	/**
	 * 抓取网页图片
	 */
	require_once '../lib/http.php';

	class action
	{
		var $uri;

		function action($uri)
		{
			$this->uri = urldecode( $uri );
		}

		function run()
		{
			$obj = new http( $this->uri );
			$phost = parse_url( $this->uri );
			$obj->set_referer( 'http://' . $phost['host'] . '/' );
			$obj->set_command(strtolower($_SERVER['REQUEST_METHOD']) == 'get' ? $_GET : $_POST, true);
			$obj->send();
			$obj->close();

			$info = pathinfo( $this->uri );
			$type = $this->get_content_type( $info );
			header( 'Content-Type: ' . $type );
			header( 'Content-Length: ' . strlen($obj->result) );
			header( 'Content-Transfer-Encoding: Binary' );
			//header( 'Content-Disposition: attachment;filename=' . time() . '.' . $info['extension'] );
			echo $obj->result;
		}

		function get_content_type( $info )
		{
			if( empty($info['extension']) ) return 'image/jpeg';
			$dict = array();
			$dict['jar'] = 'application/java-archive';
			$dict['jad'] = 'text/vnd.sun.j2me.app-descriptor';
			$dict['txt'] = 'text/plain';
			$dict['umd'] = 'multipart/related';
			$dict['gif'] = 'image/gif';
			$dict['jpg'] = 'image/jpeg';
			$dict['png'] = 'image/png';
			$dict['3gp'] = 'video/3gpp';
			$dict['mid'] = 'audio/midi';
			$dict['amr'] = 'audio/amr';
			$dict['mp3'] = 'audio/mpeg';
			$dict['wav'] = 'audio/x-wav';

			return isset( $dict[$info['extension']] ) ? $dict[$info['extension']] : 'application/octet-stream';
		}

	}

	$action = new action($_GET['uri']);
	$action->run();
?>